package ui;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

public class Keypad extends Dialog {

	protected Object result;
	protected Shell shell;

	private boolean bExitFlag = false;
	private Shell parent_shell;
	private Text text_output;
	
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Keypad(Shell parent, int style) {
		super(parent, style);
		setText("Keypad");
		parent_shell = parent;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay(); 		
		Point location = parent_shell.getLocation();		// Get the location of the parent. *SR
		shell.setLocation(location.x+68, location.y+26);
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				bExitFlag = true;
			}
		});
		btnNewButton.setBounds(251, 218, 93, 53);
		btnNewButton.setText("Done");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Times New Roman", 14, SWT.NORMAL));
		lblNewLabel.setBounds(10, 10, 334, 30);
		lblNewLabel.setText("Please enter a number using the pad below.");
		
		text_output = new Text(shell, SWT.BORDER | SWT.RIGHT);
		text_output.setFont(SWTResourceManager.getFont("Segoe UI", 18, SWT.NORMAL));
		text_output.setBounds(217, 46, 127, 48);
		
		Button btnClear = new Button(shell, SWT.NONE);
		btnClear.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text_output.setText("");
			}
		});
		btnClear.setBounds(272, 100, 72, 25);
		btnClear.setText("CLEAR");
		
		Group group = new Group(shell, SWT.NONE);
		group.setBounds(10, 35, 201, 236);
		
		Button button_9 = new Button(group, SWT.NONE);
		button_9.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"0");
			}
		});
		button_9.setBounds(75, 178, 48, 48);
		button_9.setText("0");
		
		Button button_6 = new Button(group, SWT.NONE);
		button_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"7");
			}
		});
		button_6.setBounds(10, 124, 48, 48);
		button_6.setText("7");
		
		Button button_7 = new Button(group, SWT.NONE);
		button_7.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"8");
			}
		});
		button_7.setBounds(75, 124, 48, 48);
		button_7.setText("8");
		
		Button button_8 = new Button(group, SWT.NONE);
		button_8.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"9");
			}
		});
		button_8.setBounds(140, 124, 48, 48);
		button_8.setText("9");
		
		Button button_5 = new Button(group, SWT.NONE);
		button_5.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"6");
			}
		});
		button_5.setBounds(140, 70, 48, 48);
		button_5.setText("6");
		
		Button button_4 = new Button(group, SWT.NONE);
		button_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"5");
			}
		});
		button_4.setBounds(75, 70, 48, 48);
		button_4.setText("5");
		
		Button button_3 = new Button(group, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"4");
			}
		});
		button_3.setBounds(10, 70, 48, 48);
		button_3.setText("4");
		
		Button button = new Button(group, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"1");
			}
		});
		button.setBounds(10, 16, 48, 48);
		button.setText("1");
		
		Button button_1 = new Button(group, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"2");
			}
		});
		button_1.setBounds(75, 16, 48, 48);
		button_1.setText("2");
		
		Button button_2 = new Button(group, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+"3");
			}
		});
		button_2.setBounds(140, 16, 48, 48);
		button_2.setText("3");
		
		Button button_period = new Button(group, SWT.NONE);
		button_period.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String tmp = text_output.getText();
				text_output.setText(tmp+".");
			}
		});
		button_period.setText(".");
		button_period.setBounds(140, 178, 48, 48);
		while (!shell.isDisposed() && bExitFlag == false) { // *SR
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		shell.dispose(); // *SR
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(360, 284);
		shell.setText(getText());

	}
}
